package com.shengde.jinwawaforleader.model.myJob.kingLeaderEmail;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProShowClickGood;
import com.shengde.jinwawaforleader.utils.DateUtil;
import com.shengde.jinwawaforleader.views.SelectPicPopupWindow;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * 园长信箱
 * Created by Administrator on 2016/9/1 0001.
 */
public class KingLeaderEmailActivity extends BaseActivity {
    private static final String TAG = "KingLeaderEmailActivity";
    private static final int FINSH = 1;
    private TextView tv_back;
    private TextView tv_title;
    private ListView lv_email;
    private CommonBaseAdapter commonBaseAdapter;
    private SelectPicPopupWindow showPopupWindow;
    private List<ProLeaderEmail.SmListEntity> smListEntityList;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case FINSH:
                    if (commonBaseAdapter == null) {
                        commonBaseAdapter = new CommonBaseAdapter(KingLeaderEmailActivity.this, smListEntityList, R.layout.item_leader_email) {
                            @Override
                            public void convert(ViewHolder holder, final int position) {

                                holder.setText(R.id.tv_baby_type, smListEntityList.get(position).sname);
                                holder.setText(R.id.tv_baby_name, smListEntityList.get(position).gudentname);
                                if (smListEntityList.get(position).age == 1) {
                                    holder.setText(R.id.tv_baby_age, "0-3岁");
                                } else if (smListEntityList.get(position).age == 2) {
                                    holder.setText(R.id.tv_baby_age, "3-5岁");
                                } else {
                                    holder.setText(R.id.tv_baby_age, "5岁以上");
                                }
                                holder.setText(R.id.tv_baby_time, DateUtil.getDate(String.valueOf(smListEntityList.get(position).time)));
                                holder.setText(R.id.tv_baby_tele, smListEntityList.get(position).phone);
                                if (smListEntityList.get(position).state == 0) {
                                    holder.setText(R.id.btn_request, "新申请");
                                } else if (smListEntityList.get(position).state == 1) {
                                    holder.setText(R.id.btn_request, "正在处理");
                                } else {
                                    holder.setText(R.id.btn_request, "已处理");
                                }
                                holder.setOnclickListener(R.id.btn_request, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        showPopupWindow(v,position);
                                    }
                                });

                            }
                        };
                        lv_email.setAdapter(commonBaseAdapter);
                    } else {
                        commonBaseAdapter.resetList(smListEntityList);
                        commonBaseAdapter.notifyDataSetChanged();
                    }
                    break;

                default:
                    break;
            }
        }
    };


    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.kindergrader_leader_email_activity);
    }

    @Override
    public void initView() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        lv_email = (ListView) findViewById(R.id.lv_email);
    }

    @Override
    public void initData() {
        tv_title.setText(getString(R.string.king_leader_email));
        showLeaderEmail();

    }

    /**
     * 获取信箱中的信息
     */
    private void showLeaderEmail() {
        RequestParams params = BasexUtilsProtocol.sendHttpRequest(this, "/synopsis/smsList");
        params.addParameter("schoolid", User.getSchoolId(this));
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                ProLeaderEmail proLeaderEmail = new Gson().fromJson(result, ProLeaderEmail.class);
                if (proLeaderEmail.code == 0) {
                    smListEntityList = proLeaderEmail.smList;
                    mHandler.sendEmptyMessage(FINSH);


                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                BasexUtilsProtocol.dialog.dismiss();
            }
        });
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);

    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
        }
    }

    //显示 弹窗
    private void showPopupWindow(View v, final int position) {

        showPopupWindow = new SelectPicPopupWindow(this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_video_babydiary_share:  //正在处理
                        showChangeState("1",position);
                        showPopupWindow.dismiss();
                        break;
                    case R.id.tv_video_babydiary_delete: //已处理
                        showChangeState("2",position);
                        showPopupWindow.dismiss();
                        break;
                    case R.id.tv_look_big_pic: //新申请
                        showChangeState("0",position);
                        showPopupWindow.dismiss();
                        break;
                    case R.id.tv_video_babydiary_cancle:
                        showPopupWindow.dismiss();
                        break;
                }
            }
        });
        showPopupWindow.tv_video_babydiary_share.setText("正在处理");
        showPopupWindow.tv_video_babydiary_share.setTextColor(Color.parseColor("#067DFF"));
        showPopupWindow.tv_look_big_pic.setText("新申请");
        showPopupWindow.tv_look_big_pic.setTextColor(Color.parseColor("#067DFF"));
        showPopupWindow.tv_video_babydiary_delete.setText("已处理");
        showPopupWindow.tv_video_babydiary_delete.setTextColor(Color.parseColor("#067DFF"));
        showPopupWindow.tv_video_babydiary_cancle.setTextColor(Color.parseColor("#067DFF"));

        //设置layout在PopupWindow中显示的位置
        showPopupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
    /**
     * 修改 处理信息的 状态
     */
    private void showChangeState(String state, int position) {
        String url = "";
        if (User.isTest) {
            url = BaseProtocol.HOST_BASE_TEST + "/synopsis/smsChange";
        } else {
            url = BaseProtocol.HOST_BASE + "/synopsis/smsChange";
        }
        RequestParams params = BaseProtocol.getRequestParams(url);
        params.addParameter("sid", smListEntityList.get(position).id);
        params.addParameter("schoolid", User.getSchoolId(this));
        params.addParameter("phone", smListEntityList.get(position).phone);
        params.addParameter("state", state);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                ProShowClickGood proShowClick = new Gson().fromJson(result, ProShowClickGood.class);
                if (proShowClick.getCode() == 0) {
                    showLeaderEmail();Toast.makeText(KingLeaderEmailActivity.this, "状态修改成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

}
